/**
 * Created by zhengyi.fu on 2018/8/31.
 */
import Fly from 'flyio/dist/npm/wx'
const fly = new Fly()
// const host="http://18q485b869.iok.la:47081/"
// const host="http://18q485b869.iok.la:30125/";
const host="https://xcx.saas.apis.huidianzhang.com:8080/hdz-apprest";
// const host = "http://192.168.31.68:8080"
//const host = "http://192.168.50.13:8082"
//添加请求拦截器
fly.interceptors.request.use((request) => {
  // wx.showLoading({
  //   title: "加载中",
  //   mask:true
  // });

  // request.headers["X-Tag"] = "flyio";
  // request.headers['content-type']= 'application/json';
  request.headers = {
    "X-Tag": "flyio",
    'content-type': 'application/json;charset=UTF-8'
  };

  let authParams = {
    mid: 800008
   // mid:97155
    // //公共参数
    // "categoryType": "SaleGoodsType@sim",
    // "streamNo": "wxapp153570682909641893",
    // "reqSource": "MALL_H5",
    // "appid": "string",
    // "timestamp": new Date().getTime(),
    // "sign": "string"
  };

  request.body && Object.keys(request.body).forEach((val) => {
    if(request.body[val] === ""){
      delete request.body[val]
    };
  });
  request.body = {
    ...request.body,
    ...authParams
  }
  return request;
});

//添加响应拦截器
fly.interceptors.response.use(
  (response) => {
    wx.hideLoading();
    return response.data;//请求成功之后将返回值返回
  },
  (err) => {
    //请求出错，根据返回状态码判断出错原因
    wx.hideLoading();
    console.log(err)
    if(err.message.indexOf('timeout')!=-1){
      wx.showToast({
        title: "请求超时",
        icon: 'none',
        duration: 2000
      });
    }else{
      wx.showToast({
        title: "请求失败",
        icon: 'none',
        duration: 2000
      });
    }
    if(err){
      return "请求失败";
    };
  }
);

fly.config.baseURL = host;
fly.config.timeout=10000;

export default fly;
